package com.jyw.business.ast.domain.dto.web;

import com.jyw.business.ast.constant.AstRelationConstant;
import com.jyw.business.ast.domain.AstAllocate;
import com.jyw.business.ast.domain.AstDealRelation;
import com.jyw.business.ast.domain.AstInfo;
import com.jyw.business.ast.service.IAstInfoService;
import com.jyw.common.utils.DateUtils;
import com.jyw.common.utils.SecurityUtils;
import com.jyw.common.utils.spring.SpringUtils;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.stream.Collectors;

@Data
@ApiModel("资产调拨-调拨")
public class AstAllocateDto {

    /** 调入科室 */
    @ApiModelProperty("调入科室")
    @NotNull(message = "调入科室不能为空")
    private Long inDept;

    @ApiModelProperty("调拨原因")
    private String reason;

    @ApiModelProperty("资产条目(资产id集合)")
    private List<Long> astList;

    /**
     * 封装调拨信息
     */
    public AstAllocate buildAllocate() {
        AstAllocate allocate = new AstAllocate();
        allocate.setInDept(getInDept());
        allocate.setReason(getReason());
        allocate.setCreateUser(SecurityUtils.getUserId());
        allocate.setCreateTime(DateUtils.getNowDate());
        return allocate;
    }

    /**
     * 封装调拨关系
     * @param allocate
     */
    public List<AstDealRelation> buildRelation(AstAllocate allocate) {
        IAstInfoService service = SpringUtils.getBean(IAstInfoService.class);
        return astList.stream().map(data -> {
            AstDealRelation relation = new AstDealRelation();
            relation.setType(AstRelationConstant.ALLOCATE);
            //获取资产信息
            AstInfo astInfo = service.getById(data);
            relation.setOutDept(astInfo.getUseDept());
            relation.setInDept(allocate.getInDept());
            relation.setBusinessId(allocate.getId());
            relation.setAstId(data);
            return relation;
        }).collect(Collectors.toList());
    }

    /**
     * 设置修改使用科室
     */
    public List<AstInfo> buildAstInfo() {
        return astList.stream().map(data -> {
            AstInfo astInfo = new AstInfo();
            astInfo.setId(data);
            astInfo.setUseDept(getInDept());
            return astInfo;
        }).collect(Collectors.toList());

    }
}
